define(['jquery', 'bootstrap', 'backend', 'table', 'form','vue','template'], function ($, undefined, Backend, Table, Form,Vue,Template) {
    //vue
    function initSaveVue() {
        var vue = new Vue({
            el:'#index_div',
            data:{
                province_list: [],
                city_list: [],
                county_list: [],
                village_list: [],
                province_id : '',
                city_id : '',
                county_id : '',
                village_id : '',
                building_number : '',
                floor_number : '',
                room_number : '',
                water_charge : '',
                property_charge : '',
                detail : '',
            },
            watch:{
                province_id:function (val) {
                    this.province_id = val;
                    this.getCityList();
                    this.village_list = [];
                },
                city_id:function (val) {
                    this.city_id = val;
                    this.getCountyList();
                    this.village_list = [];
                },
                county_id:function (val) {
                    this.county_id = val;
                    this.getVillageList();
                }
            },
            methods: {
                getProvinceList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : 0
                    }, function (res) {
                        that.province_list = res;
                    });
                },
                getCityList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.province_id
                    }, function (res) {
                        that.city_list = res;
                    });
                },
                getCountyList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.city_id
                    }, function (res) {
                        that.county_list = res;
                    });
                },
                getVillageList:function(){
                    var that = this;
                    $.get("adminpublic/ajax_village_list", {
                        county_id : that.county_id
                    }, function (res) {
                        that.village_list = res;
                    });
                }
            }
        });
        return vue;
    }

    function initSearchVue() {
        var vue = new Vue({
            el:'#search_div',
            data:{
                province_list: [],
                city_list: [],
                county_list: [],
                village_list: [],
                province_id : '',
                city_id : '',
                county_id : '',
                village_id : '',
            },
            watch:{
                province_id:function (val) {
                    this.province_id = val;
                    this.getCityList();
                    this.village_list = [];
                },
                city_id:function (val) {
                    this.city_id = val;
                    this.getCountyList();
                    this.village_list = [];
                },
                county_id:function (val) {
                    this.county_id = val;
                    this.getVillageList();
                }
            },
            methods: {
                getProvinceList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : 0
                    }, function (res) {
                        that.province_list = res;
                        console.info('ccc');
                    });
                },
                getCityList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.province_id
                    }, function (res) {
                        that.city_list = res;
                    });
                },
                getCountyList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.city_id
                    }, function (res) {
                        that.county_list = res;
                    });
                },
                getVillageList:function(){
                    var that = this;
                    $.get("adminpublic/ajax_village_list", {
                        county_id : that.county_id
                    }, function (res) {
                        that.village_list = res;
                    });
                }
            }
        });
        vue.getProvinceList();
        return vue;
    }
    function doDept($ids) {
        Layer.confirm("是否确认发送催款信息？", function () {
            $.ajax({
                type:"get"
                ,url:"houseproperty/dept/ids/"+$ids
                ,error:function (data) {
                    console.info(data);
                    Toastr.error('服务器异常');
                    Layer.closeAll();
                }
                ,success:function (data) {
                    if(data.code == 1){
                        Toastr.success(data.msg);
                    }
                    else{
                        Toastr.error(data.msg);
                    }
                    Layer.closeAll();
                }
            });
        });
    }
    var Controller = {
        index: function () {
            var owner_id = Config.owner_id;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'houseproperty/index/ids/'+owner_id,
                    add_url: 'houseproperty/add/ids/'+owner_id,
                    edit_url: 'houseproperty/edit',
                    del_url: 'houseproperty/del',
                    multi_url: 'houseproperty/multi',
                }
            });

            var table = $("#table");
            table.on('common-search.bs.table', function (event, table, query) {
                console.info('bbb');
            });

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                console.info('aaa');
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                search:false,
                columns: [
                    [
                        {field: 'state', checkbox: true },
                        {field: 'id', title: 'ID'},
                        {field: 'village_name', title: __('Village name'),searchable:false},
                        {field: 'building_number', title: __('Building number'),searchable:false},
                        {field: 'floor_number', title: __('Floor number'),searchable:false},
                        {field: 'room_number', title: __('Room number'),searchable:false},
                        {field: 'detail', title: __('Detail'),searchable:false},
                        {field: 'water_charge', title: __('Water charge'),searchable:false},
                        {field: 'property_charge', title: __('Property charge'),searchable:false},
                        {field: 'charge_total', title: __('Charge total'),searchable:false},
                        {field: 'create_time', title: __('Create time'),
                            formatter: Table.api.formatter.datetime, operate: 'BETWEEN',
                            type: 'datetime', addclass: 'datetimepicker',
                            data: 'data-date-format="YYYY-MM-DD"'
                        },
                        {field: 'notice_time', title: __('Dept'),events: Controller.api.events.dept,formatter:function(value,row,index){
                            if(row.charge_total > 0){
                                return '<a class="btn btn-default btn-xs btn-dept">' + __('Dept') + '</a>';
                            }else{
                                return '';
                            }
                        }},
                        {field: 'village_id', title: __('Village'),visible:false,searchList: function (column) {
                            return Template('village_search', {});
                        }
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function (params) {
                    console.info(params);
                    return params;
                },
            });
            $(document).on("click", ".btn-dept-selected", function () {
                var ids = Table.api.selectedids(table);
                doDept(ids);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            initSearchVue();

        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = initSaveVue();
            vue.getProvinceList();
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = initSaveVue();
            vue.getProvinceList();
            var house_property = Config.house_property;
            vue.province_id = Config.village.province_id;
            vue.city_id = Config.village.city_id;
            vue.county_id = Config.village.county_id;
            vue.village_id = house_property.village_id;
            vue.building_number = house_property.building_number;
            vue.floor_number = house_property.floor_number;
            vue.room_number = house_property.room_number;
            vue.water_charge = house_property.water_charge;
            vue.property_charge = house_property.property_charge;
            vue.detail = house_property.detail;
        },api: {
            events: {
                dept:{
                    'click .btn-dept': function (e, value, row, index) {
                        doDept(row.id);
                    }
                }
            }
        }

    };
    return Controller;
});